{% extends "base.html" %}
{% block content %}
    <h1>Penalties</h1>
   {% if current_user.is_admin() or current_user.is_editor() %}
        <a href="{{ url_for('upload') }}">Upload Excel File</a>
    {% endif %}

    <!-- 搜索表单 -->
    <form method="GET" action="{{ url_for('index') }}" class="search-form">
        <div>
            <label for="case_no">Case No:</label>
            <input type="text" id="case_no" name="case_no" value="{{ request.args.get('case_no', '') }}" placeholder="Enter case number">
        </div>
        <div>
            <label for="judge_date_start">Judge Date Start:</label>
            <input type="date" id="judge_date_start" name="judge_date_start" value="{{ request.args.get('judge_date_start', '') }}">
        </div>
        <div>
            <label for="judge_date_end">Judge Date End:</label>
            <input type="date" id="judge_date_end" name="judge_date_end" value="{{ request.args.get('judge_date_end', '') }}">
        </div>
        <div>
            <button type="submit">Search</button>
            <a href="{{ url_for('index') }}" class="reset-button">Reset</a>
        </div>
    </form>

    <!-- 展示搜索结果 -->
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Case No</th>
                <th>Counterpart</th>
                <th>Judge Date</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for penalty in penalties.items %}
                <tr>
                    <td>{{ penalty.id }}</td>
                    <td>{{ penalty.case_no }}</td>
                    <td>{{ penalty.counterpart }}</td>
                    <td>{{ penalty.judge_date }}</td>
                    <td>
                        <a href="{{ url_for('detail', penalty_id=penalty.id) }}">View/Edit</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    {% if current_user.is_admin() or current_user.is_editor() %}
    <!-- 分页导航 -->
    <div class="pagination">
        {% if penalties.has_prev %}
            <a href="{{ url_for('index', page=penalties.prev_num, case_no=request.args.get('case_no'), judge_date_start=request.args.get('judge_date_start'), judge_date_end=request.args.get('judge_date_end')) }}">Previous</a>
        {% endif %}

        {% for page_num in penalties.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
            {% if page_num %}
                {% if penalties.page == page_num %}
                    <strong>{{ page_num }}</strong>
                {% else %}
                    <a href="{{ url_for('index', page=page_num, case_no=request.args.get('case_no'), judge_date_start=request.args.get('judge_date_start'), judge_date_end=request.args.get('judge_date_end')) }}">{{ page_num }}</a>
                {% endif %}
            {% else %}
                <span>...</span>
            {% endif %}
        {% endfor %}

        {% if penalties.has_next %}
            <a href="{{ url_for('index', page=penalties.next_num, case_no=request.args.get('case_no'), judge_date_start=request.args.get('judge_date_start'), judge_date_end=request.args.get('judge_date_end')) }}">Next</a>
        {% endif %}
    </div>
{% endif %}
{% endblock %}